package com.zhoug.common.network.ssl;

import android.content.Context;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;

/**
 * 秘钥管理器工厂
 * 创建自己的证书储存仓库管理器
 * 生成自己的证书命令:
 * 1.生成证书库,放入自己的app中
 * keytool -validity 3650 -genkeypair -v -alias client -keyalg RSA -storetype PKCS12 -keystore E:\Desktop\证书生成\client.p12 -dname "CN=heking,OU=zg,O=zg,L=chongqing,ST=chongqing,c=cn" -storepass hk123*456 -keypass hk123*456
 * 2.从证书库中导出证书,给服务器端用于验证
 * keytool -export -v -alias client -keystore E:\Desktop\证书生成\client.p12 -storetype PKCS12 -storepass hk123*456 -rfc -file E:\Desktop\证书生成\client.cer
 *
 * @Author 35574
 * @Date 2021/10/12
 * @Description
 */
public class ZKeyManagerFactory {

    /**
     * 获取KeyManager
     * @param context
     * @param assetsPath
     * @param password
     * @return
     */
    public static KeyManager[] getKeyManagers(Context context, String assetsPath, String password) {
        try {
            if (assetsPath == null) {
                return null;
            }
            KeyStore keyStore = KeyStore.getInstance(SSLConstants.KEY_STORE_TYPE_P12);
            keyStore.load(context.getAssets().open(assetsPath), password.toCharArray());
            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(SSLConstants.CERTIFICATE_TYPE_X509);
            keyManagerFactory.init(keyStore, password.toCharArray());
            return keyManagerFactory.getKeyManagers();
        } catch (NoSuchAlgorithmException | KeyStoreException | IOException | CertificateException | UnrecoverableKeyException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *  获取KeyManager
     * @param inputStream
     * @param password
     * @return
     */
    public static KeyManager[] getKeyManagers(InputStream inputStream, String password) {
        try {
            if (inputStream == null) {
                return null;
            }
            KeyStore keyStore = KeyStore.getInstance(SSLConstants.KEY_STORE_TYPE_P12);
            keyStore.load(inputStream, password.toCharArray());
            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(SSLConstants.CERTIFICATE_TYPE_X509);
            keyManagerFactory.init(keyStore, password.toCharArray());
            return keyManagerFactory.getKeyManagers();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     *  获取KeyManager
     * @param absKeyPath
     * @param password
     * @return
     */
    public static KeyManager[] getKeyManagers(String absKeyPath, String password) {
        try {
            if (absKeyPath == null) {
                return null;
            }
            File file=new File(absKeyPath);
            if(file.exists()){
                return getKeyManagers(new FileInputStream(absKeyPath),password);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
